package com.twu.biblioteca.io;

import com.twu.biblioteca.session.UserSession;
import com.twu.biblioteca.core.Library;
import com.twu.biblioteca.core.User;
import com.twu.biblioteca.exceptions.InvalidBookOperationException;
import com.twu.biblioteca.exceptions.InvalidMovieOperationException;

/**
 * Created by cristianpinto on 2/19/14.
 */
public class MainMenu {

    private UserIOParser userIOParser;
    private Library library;
    private UserSession userSession;

    public MainMenu (UserIOParser userIOParser, Library library, UserSession userSession) {
        this.userIOParser = userIOParser;
        this.library = library;
        this.userSession = userSession;
    }

    public void greet() {
        userIOParser.printLine("Hi welcome to The Bangalore Public Library Application");
    }

    public void showMenu() {

        boolean continueLoop = true;
        while (continueLoop) {
            userIOParser.printLine("Please, type one of the following: ");
            userIOParser.printLine("Log in");
            userIOParser.printLine("Book list");
            userIOParser.printLine("Check out book");
            userIOParser.printLine("Return book");
            userIOParser.printLine("Movie list");
            userIOParser.printLine("Check out movie");
            userIOParser.printLine("Show my info");
            userIOParser.printLine("Exit");

            String option = userIOParser.readUserInput();
            if ("Log in".equals(option)){
                userIOParser.printLine("Library number: ");
                String libraryNumber = userIOParser.readUserInput();
                userIOParser.printLine("Password: ");
                String password = userIOParser.readUserInput();
                if(userSession.logIn(libraryNumber, password)) {
                    userIOParser.printLine("Welcome!");
                } else {
                    userIOParser.printLine("Invalid library number or password");
                }
            } else if("Book list".equals(option)) {
                userIOParser.printLine(library.getAvailableBooks());
            } else if("Movie list".equals(option)) {
                userIOParser.printLine(library.getAvailableMovies());
            } else if ("Check out book".equals(option)) {
                checkOutBookAction();
            } else if ("Check out movie".equals(option)) {
                checkOutMovieAction();
            } else if ("Return book".equals(option)) {
                checkInBookAction();
            } else if ("Show my info".equals(option)) {
                User currentUser = userSession.getCurrentUser();
                if (currentUser != null)
                    userIOParser.printLine(currentUser.getUserInformation());
                else
                    userIOParser.printLine("You must be logged in");
            }else if ("Exit".equals(option)){
                continueLoop = false;
            } else {
                userIOParser.printLine("Invalid option");
            }

        }
    }

    private void checkOutMovieAction() {
        userIOParser.printLine("Movie name?");
        String movieName = userIOParser.readUserInput();
        try {
            library.checkoutMovie(movieName);
            userIOParser.printLine("Enjoy");
        } catch (InvalidMovieOperationException exception) {
            userIOParser.printLine(exception.getMessage());
        }
    }

    private void checkOutBookAction() {
        User currentUser = userSession.getCurrentUser();
        if(currentUser != null) {
            userIOParser.printLine("Book name?");
            String title = userIOParser.readUserInput();
            try {
                library.checkOutBook(title, currentUser);
                userIOParser.printLine("Happy reading");
            } catch (InvalidBookOperationException exception) {
                userIOParser.printLine(exception.getMessage());
            }
        } else {
            userIOParser.printLine("You must be logged in");
        }
    }

    private void checkInBookAction() {
        if (userSession.getCurrentUser() != null) {
            userIOParser.printLine("Book name?");
            String title = userIOParser.readUserInput();
            try {
                library.checkInBook(title);
                userIOParser.printLine("Thank you");
            } catch (InvalidBookOperationException exception) {
                userIOParser.printLine(exception.getMessage());
            }
        } else {
            userIOParser.printLine("You must be logged in");
        }
    }
}